﻿using CoffeeShop.BIZ;
using CoffeeShop.Common;
using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CoffeeShop.Controllers
{
    public class TableController : Controller
    {
        private TableBiz _tableBiz = new TableBiz();
        private OfficeBiz _officeBiz = new OfficeBiz();
        private AreaBiz _areaBiz = new AreaBiz();

        /// <summary>
        /// List table page
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Admin, Staff")]
        public ActionResult Index()
        {
            List<Table> listTables = new List<Table>();
            listTables = _tableBiz.GetAllTables();
            return View(listTables);
        }

        /// <summary>
        /// Create a new table
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Admin, Staff")]
        public string CreateTable()
        {
            string tableName = Request.Params["tableName"].Trim();
            string officeId = Request.Params["officeId"].Trim();
            string areaId = Request.Params["areaId"].Trim();
            var isExistedTable = _tableBiz.IsDuplicateTableCreate(tableName, Convert.ToInt32(officeId), Convert.ToInt32(areaId));
            if (isExistedTable)
            {
                return "ExistedTable";
            }
            else
            {
                Table table = new Table();
                table = _tableBiz.ConvertToTableObject(tableName, Convert.ToInt32(officeId), Convert.ToInt32(areaId));
                if (_tableBiz.CreateTable(table))
                {
                    return "CreateSuccessful";
                }
                else
                {
                    return "CreateFail";
                }
            }
        }

        /// <summary>
        /// Update an table
        /// </summary>
        [Authorize(Roles = "Admin, Staff")]
        public string UpdateTable()
        {
            string tableId = Request.Params["tableId"].Trim();
            string tableName = Request.Params["tableName"].Trim();
            string officeId = Request.Params["officeId"].Trim();
            string areaId = Request.Params["areaId"].Trim();

            if (_tableBiz.IsDuplicateTableWhenUpdate(Convert.ToInt32(tableId), tableName, Convert.ToInt32(officeId), Convert.ToInt32(areaId)))
            {
                return "ExistedTableName";
            }
            Table table = new Table();
            table = _tableBiz.ConvertToTableObject(tableName, Convert.ToInt32(officeId), Convert.ToInt32(areaId), Convert.ToInt32(tableId));
            if (_tableBiz.UpdateTable(table))
            {
                return "UpdateSuccessful";
            }
            else
            {
                return "FailUpdateTable";
            }
        }

        /// <summary>
        /// Delete an office in Database
        /// </summary>
        /// <param name="officeId"></param>
        /// <returns></returns>
        public string DeleteTable(string tableId)
        {
            var isDeleteSuccess = _tableBiz.DeleteTable(Convert.ToInt32(tableId));
            if (isDeleteSuccess)
            {
                return "DeleteSuccessful";
            }
            else
            {
                return "FailDeleteTable";
            }
        }

        /// <summary>
        /// Get table by Id
        /// </summary>
        /// <param name="tableId"></param>
        /// <returns></returns>
        public JsonResult GetTableById(string tableId)
        {
            Table table = new Table();
            table = _tableBiz.GetTableById(Convert.ToInt32(tableId));
            return Json(new { TableId = table.TableId, Name = table.Name, OfficeId = table.OfficeId, AreaId = table.AreaId, IsDeleted = table.IsDeleted }
                , JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Get list office to send value to object
        /// </summary>
        /// <returns></returns>
        public JsonResult GetListOffice()
        {
            List<Office> listOffice = new List<Office>();
            listOffice = _officeBiz.GetListActiveOffice();

            List<Object> listResult = new List<Object>();
            foreach (var office in listOffice)
            {
                var result = CmnFunc.ConvertDynamicObjectWithFullAttr(office);
                listResult.Add(result);
            }
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Get list areas by OfficeId
        /// </summary>
        /// <param name="officeId"></param>
        /// <returns>List of areas</returns>
        public JsonResult GetListAreasByOfficeId(int officeId)
        {
            List<Area> listArea = new List<Area>();
            listArea = _areaBiz.GetListAreasByOfficeId(officeId);

            List<Object> listResult = new List<object>();
            foreach (var area in listArea)
            {
                var result = CmnFunc.ConvertDynamicObjectWithFullAttr(area);
                listResult.Add(result);
            }
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public PartialViewResult LoadAllTables()
        {
            List<Table> listAllTables = new List<Table>();
            listAllTables = _tableBiz.GetAllTables();
            return PartialView("_TableList", listAllTables);
        }
    }
}
